package Util::Tuan::TuansCreate;
use Image::Magick;
use Moose;
use namespace::autoclean;
use Image::Magick;
use File::Basename;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub create {
	my ($self, $image, $static, $name, $description, $comm_id, $user_id, $storeIds, $prices,  $group_qitian, $group_tiyan, $group_zhifu, $tihuo_df, $tihuo_dt, $tihuo_location, $tiyan_df, $tiyan_dt, $tiyan_location, $universities, $taobao_link, $oldimage, $receive_notice, $yuji_tihuo_time, $dingqi) = @_;

	my ($t_filename, $tmp_target, $small_image, $big_image, $f_suffix);

        if($image){
		my($filename, $directories, $suffix) = fileparse($image->filename, qr/\.[^.]*/);
                $f_suffix = $suffix;
                my $rand = new String::Random;
                $t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

                my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
                $year = 1900 + $year;
                $mon = 1 + $mon;
                my $dir = "/$year/$mon/$mday";

                $t_filename = $t_filename . $suffix;
                $tmp_target = "/tmp/$dir/$t_filename";

                system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
                system("mkdir -p \"$static/small/$dir\"") unless(-e "$static/small/$dir");
                system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
                $small_image = "$static/small/$dir/$t_filename";
                $big_image = "$static/big/$dir/$t_filename";
                $t_filename = "$dir/$t_filename";

                unless ( $image->link_to($tmp_target) || $image->copy_to($tmp_target) ) {
                        die( "Failed to copy '$filename' to '$tmp_target': $!" );
                }

                $f_suffix =~ s/\.//;

		my $t = new Image::Magick::Thumbnail::Fixed;
                $t->thumbnail( input   => $tmp_target,
                                output  => $small_image,
                                width   => 279,
                                height  => 212,
				density => '279x212',
                                gravity => 'center',
                                compose => 'over',
                                quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix );

                $t->thumbnail( input   => $tmp_target,
                                output  => $big_image,
                                width   => 630,
                                height  => 479,
                                density => '630x479',
                                gravity => 'center',
                                compose => 'over',
                                quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix );

	} else {
		if($oldimage eq "0") {
			$t_filename = "tuan/default.jpg";
		} else {
			$t_filename = $oldimage;
		}
	
        }

        my $tuan = $self->schema->resultset('Tuan')->create({
                        name => $name,
                        description => $description,
                        comm_item_id => $comm_id,
                        image => $t_filename,
			pay_url => $taobao_link,
                        status => 'A',
			tihuo_time_from => $tihuo_df->datetime,
			tihuo_time_to => $tihuo_dt->datetime,
			tihuo_location => $tihuo_location,
			yuji_tihuo_time => $yuji_tihuo_time,
			tiyan_time_from => $tiyan_df->datetime,
			tiyan_time_to => $tiyan_dt->datetime,
			tiyan_location => $tiyan_location,
			qitian	=> $group_qitian,
			tiyan => $group_tiyan,
			counter => 0,
			dingqi => $dingqi,
			zhifu => $group_zhifu,
			receive_notice => $receive_notice
                        });

#TODO changed to real category
        #$tuan->add_to_tuan_category({category_id => $category});

        $tuan->add_to_user_tuan({
                user_id => $user_id,
                role=>'C',
        });

	my $count = scalar(@$storeIds);
	while($count > 0) {
		my $storeId = $storeIds->[$count - 1];
		my $price = $prices->[$count - 1];
		$tuan->add_to_tuan_stores({
			stores_id => $storeId,
			price => $price
		});
		$count = $count - 1;
	}

	$count = scalar(@$universities);
	while($count > 0) {
		my $university = $universities->[$count - 1];
		$tuan->add_to_university_tuan({
			university_id => $university
		});
		$count = $count - 1;
	}

	return $tuan;
}


sub update_status {
	my ($self, $status, $tuan_id) = @_;
        my $tuan = $self->schema->resultset('Tuan')->find($tuan_id);
	$tuan->update({
		status => $status,
		#zhifu_eff_to => $one_week_later,
		#fahuo_eff_to => $nine_days_later
		zhifu_eff_to => $tuan->tihuo_time_to,
		fahuo_eff_to => $tuan->tihuo_time_to
	});
	return $tuan;
}

sub update_counter {
	my ($self, $tuan) = @_;
	my $counter = $tuan->counter + 1; 
	$tuan->update({
		counter => $counter,
	});
}

sub update {
	my ($self, $image, $static, $name, $description, $comm_id, $user_id, $storeIds, $prices,  $group_qitian, $group_tiyan, $group_zhifu, $tihuo_df, $tihuo_dt, $tihuo_location, $tiyan_df, $tiyan_dt, $tiyan_location, $universities, $tuan_id, $yuji_tihuo_time, $dingqi) = @_;
	#my ($self, $image, $static, $name, $description, $comm_id, $user_id, $storeIds, $prices,  $group_qitian, $group_tiyan, $group_zhifu, $tihuo_df, $tihuo_dt, $tiyan_df, $tiyan_dt, $tiyan_location, $universities, $tuan_id) = @_;

	my ($t_filename, $tmp_target, $small_image, $big_image, $f_suffix);

        my $tuan = $self->schema->resultset('Tuan')->find($tuan_id);
        if($image){
		my($filename, $directories, $suffix) = fileparse($image->filename, qr/\.[^.]*/);
                $f_suffix = $suffix;
                my $rand = new String::Random;
                $t_filename = $rand->randpattern("CCCCCcccccnnnnncccccCCCCC");

                my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
                $year = 1900 + $year;
                $mon = 1 + $mon;
                my $dir = "/$year/$mon/$mday";

                $t_filename = $t_filename . $suffix;
                $tmp_target = "/tmp/$dir/$t_filename";

                system("mkdir -p \"/tmp/$dir\"") unless(-e "/tmp/$dir");
                system("mkdir -p \"$static/small/$dir\"") unless(-e "$static/small/$dir");
                system("mkdir -p \"$static/big/$dir\"") unless(-e "$static/big/$dir");
                $small_image = "$static/small/$dir/$t_filename";
                $big_image = "$static/big/$dir/$t_filename";
                $t_filename = "$dir/$t_filename";

                unless ( $image->link_to($tmp_target) || $image->copy_to($tmp_target) ) {
                        die( "Failed to copy '$filename' to '$tmp_target': $!" );
                }

                $f_suffix =~ s/\.//;

		my $t = new Image::Magick::Thumbnail::Fixed;
                $t->thumbnail( input   => $tmp_target,
                                output  => $small_image,
                                width   => 279,
                                height  => 212,
				density => '279x212',
                                gravity => 'center',
                                compose => 'over',
                                quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix );

                $t->thumbnail( input   => $tmp_target,
                                output  => $big_image,
                                width   => 630,
                                height  => 479,
                                density => '630x479',
                                gravity => 'center',
                                compose => 'over',
                                quality => '90',
                                bgcolor => 'white',
                                format  => $f_suffix );

	} else {
		$t_filename = $tuan->image;
        }

	if($group_tiyan eq 'y') {
		$tuan->update({
			tiyan_time_from => $tiyan_df->datetime,
			tiyan_time_to => $tiyan_dt->datetime,
			tiyan_location => $tiyan_location
		});
	}
	$tuan->update({
		name => $name,
                description => $description,
                comm_item_id => $comm_id,
                image => $t_filename,
                status => 'A',
                tihuo_time_from => $tihuo_df->datetime,
                tihuo_time_to => $tihuo_dt->datetime,
                tihuo_location => $tihuo_location,
                yuji_tihuo_time => $yuji_tihuo_time,
                qitian  => $group_qitian,
                tiyan => $group_tiyan,
                dingqi => $dingqi,
                zhifu => $group_zhifu
	});

	my $count = scalar(@$storeIds);
	
	my @stores = $tuan->stores;
	foreach (@stores) {
		$tuan->remove_from_stores($_);
	}
	while($count > 0) {
		my $storeId = $storeIds->[$count - 1];
		my $price = $prices->[$count - 1];
		$tuan->add_to_tuan_stores({
			stores_id => $storeId,
				price => $price
		});
		$count = $count - 1;
	}

	my @unis = $tuan->university;
	foreach (@unis) {
		$tuan->remove_from_university($_);
	}
	$count = scalar(@$universities);
	while($count > 0) {
		my $university = $universities->[$count - 1];
		$tuan->add_to_university_tuan({
			university_id => $university
		});
		$count = $count - 1;
	}

	return $tuan;
}

__PACKAGE__->meta->make_immutable;

1;
